package com.sanley.coronavirus.controller;

import com.github.pagehelper.PageInfo;
import com.sanley.coronavirus.bo.PatientBO;
import com.sanley.coronavirus.common.api.CommonResult;
import com.sanley.coronavirus.entity.Patient;
import com.sanley.coronavirus.entity.Touch;
import com.sanley.coronavirus.entity.User;
import com.sanley.coronavirus.service.PatientService;
import com.sanley.coronavirus.service.TouchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.sql.Date;
import java.util.List;

@Api(value = "TouchController", description = "密接管理")
@Controller
@RequestMapping("/touch")
public class TouchController {
    @Autowired
    TouchService service;

    @Autowired
    PatientService patientService;


    //隔离完成
    @ApiOperation("隔离完成")
    @PostMapping(value = "/toSafe")
    @ResponseBody
    public CommonResult toSafe(@RequestParam(name = "baseId") Integer baseId) {
        service.toSafe(baseId);
        return CommonResult.success(null);
    }

    //查看现存密切接触者
    @ApiOperation("查看现存密切接触者")
    @GetMapping(value = "/list")
    @ResponseBody
    public CommonResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                             @RequestParam(name = "size", defaultValue = "25") int size,
                             @RequestParam(name = "keyword", required = false) String keyword) {
        List<Touch> touches;
        if (keyword != null && keyword.trim().length() != 0) {
            touches = service.findByName(keyword);
        }else {
            touches = service.findAll(page, size);
        }
        PageInfo pageInfo = new PageInfo(touches);
        return CommonResult.success(pageInfo);
    }


    //添加密切
    @ApiOperation("添加密切")
    @PostMapping(value = "/add")
    @ResponseBody
    public CommonResult<String> addPatient(@RequestBody PatientBO patientBO, Principal principal) {
        service.addTouch(patientBO, principal.getName());
        return CommonResult.success("添加成功");
    }
}
